package javagprssrv;

import java.io.Writer;
import java.io.File;
import javagprssrv.Common.DateTime;
import java.io.PrintWriter;
import java.io.FileWriter;

public class Log
{
    static java.io.FileWriter fw;
    static java.io.PrintWriter pw;
    
    public java.lang.String String2HexString(final java.lang.String strMain, final java.lang.String strSpace) {
        java.lang.String ret1 = "";
        java.lang.String ret2 = "";
        if (strMain.length() == 0) {
            ret2 = "";
            return ret2;
        }
        for (int i = 0; i < strMain.length(); ++i) {
            final int c = strMain.charAt(i);
            java.lang.String sc = "";
            if (c <= 15) {
                sc = "0" + java.lang.Integer.toHexString(c).toUpperCase();
            }
            else {
                sc = java.lang.Integer.toHexString(c).toUpperCase();
            }
            ret1 += sc;
        }
        for (int i = 0; i < ret1.length() / 2; ++i) {
            ret2 = ret2 + ret1.substring(i * 2, i * 2 + 2) + strSpace;
        }
        ret2 = ret2.substring(0, ret2.length() - strSpace.length());
        return ret2;
    }
    
    public synchronized void ShowLog(java.lang.String LogString, final java.lang.Boolean isSave, final java.lang.Boolean isShowTime) {
        final java.lang.String FileName = "";
        java.lang.String strTime = "";
        if (isShowTime) {
            strTime = new javagprssrv.Common.DateTime().getDateTime("[YY-MM-DD HH:NN:SS] ");
            LogString = strTime + LogString;
        }
        if (javagprssrv.JGS_Param.isServerShowLog) {
            java.lang.System.out.println(LogString);
        }
        try {
            for (int i = 0; i < javagprssrv.JGS_Param.ClientPipe.size(); ++i) {
                final java.io.PrintWriter o = (PrintWriter) javagprssrv.JGS_Param.ClientPipe.elementAt(i);
                o.println("");
                o.print(LogString);
                o.flush();
            }
        }
        catch (java.lang.Exception ex) {}
        final java.io.File f = new java.io.File("DataLog\\log" + javagprssrv.JGS_Param.LogFileName + ".txt");
        if (!f.exists()) {
            try {
                f.createNewFile();
                javagprssrv.Log.fw = null;
                javagprssrv.Log.pw = null;
            }
            catch (java.lang.Exception ex2) {}
        }
        try {
            if (javagprssrv.Log.fw == null) {
                javagprssrv.Log.fw = new java.io.FileWriter("DataLog\\log" + javagprssrv.JGS_Param.LogFileName + ".txt", true);
                javagprssrv.Log.pw = new java.io.PrintWriter(javagprssrv.Log.fw);
            }
            javagprssrv.Log.pw.println(LogString);
            javagprssrv.Log.pw.flush();
        }
        catch (java.lang.Exception ex3) {}
    }
    
    static {
        javagprssrv.Log.fw = null;
        javagprssrv.Log.pw = null;
    }
}
